<?php
namespace common\modules\bookmark\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use common\modules\user\models\User;
use common\modules\bookmark\models\query\BookmarkQuery;
use yii\behaviors\BlameableBehavior;

/**
 * This is the model class for table "{{%bookmark}}".
 *
 * @property integer $id
 * @property integer $user_id
 * @property integer $type
 * @property string $name
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property User $user
 * @property BookmarkItem[] $bookmarkItems
 *
 * @author Xuegui Yuan <yuanxuegui@163.com>
 */
class Bookmark extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%bookmark}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'user_id',
                'updatedByAttribute' => false
            ],
            [
                'class' => TimestampBehavior::className()
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'type',
                    'name'
                ],
                'required'
            ],
            [
                [
                    'user_id',
                    'object_count',
                    'status'
                ],
                'integer'
            ],
            [
                [
                    'name'
                ],
                'string',
                'max' => 50
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'user_id' => Yii::t('common', 'User ID'),
            'type' => Yii::t('common', 'Type'),
            'name' => Yii::t('common', 'Name'),
            'status' => Yii::t('common', 'Status'),
            'created_at' => Yii::t('common', 'Created At'),
            'updated_at' => Yii::t('common', 'Updated At')
        ];
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), [
            'id' => 'user_id'
        ]);
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getBookmarkItems()
    {
        return $this->hasMany(BookmarkItem::className(), [
            'bookmark_id' => 'id'
        ]);
    }

    /**
     * @inheritdoc
     *
     * @return \common\modules\bookmark\models\query\BookmarkQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new BookmarkQuery(get_called_class());
    }
}
